﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DA
{
    // màu sắc
    public class MauSacDA : DBConnection
    {
        // hàm tạo
        public MauSacDA()
        {
            sqlCommand.Connection = sqlConnection;
        }
        // lấy tất cả
        public DataSet GetAll()
        {
            var ds = new DataSet();
            var query = "select * from MauSac";
            if (open())
            {
                sqlDataAdapter = new SqlDataAdapter(query, sqlConnection);
                sqlDataAdapter.Fill(ds);
            }
            close();
            return ds;
        }
        // thêm 
        public int Add(MauSacDTO item)
        {
            var query = String.Format("insert into MAUSAC values(N'{0}'); select SCOPE_IDENTITY();",
                item.TenMauSac);
            sqlCommand.CommandText = query;
            try
            {
                open();
                var idRe = -1;
                sqlCommand.CommandText = query;
                var re = sqlCommand.ExecuteScalar().ToString();
                idRe = Int32.Parse(re);
                close();
                return idRe;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return -1;
            }
        }
        // sửa
        public bool Update(MauSacDTO item)
        {
            var query = String.Format("Update MAUSAC set TenMauSac = N'{0}' where MaMauSac = {1}",
                                      item.TenMauSac, item.MaMauSac);
            sqlCommand.CommandText = query;

            try
            {
                open();
                sqlCommand.ExecuteNonQuery();
                sqlCommand.Parameters.Clear();
                close();
                return true;
            }
            catch (SqlException s)
            {
                Console.WriteLine(s.Message);
                return false;
            }
        }
        // xóa 
        public bool Delete(int ma_item)
        {
            var query = "Delete from MAUSAC where MaMauSac = " + ma_item;
            sqlCommand.CommandText = query;
            try
            {
                open();
                sqlCommand.ExecuteNonQuery();
                sqlCommand.Parameters.Clear();
                close();
                return true;
            }
            catch (SqlException s)
            {
                Console.WriteLine(s.Message);
                return false;
            }
        }
        public int getMax_MaMauSac()
        {
            const string _getMasoluong = "select MAX(MaMauSac) from MauSac";
            return new MyQueryDA().getId(_getMasoluong);
        }
    }
}
